package com.artqiyi.dollmachine.views.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;
import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.common.Base.BaseActivity;
import com.artqiyi.dollmachine.model.entity.CoinBuyBean;
import com.artqiyi.dollmachine.model.entity.CoinRecordBean;
import com.artqiyi.dollmachine.model.entity.CoinTotalBean;
import com.artqiyi.dollmachine.presenter.base.MinePayRecordPresenter;
import com.artqiyi.dollmachine.presenter.base.PresenterCallbackCollection;
import com.artqiyi.dollmachine.utils.manager.PayDialogManager;
import com.artqiyi.dollmachine.utils.manager.UserInfoManager;
import com.artqiyi.dollmachine.views.adapter.MineDollCoinAdapter;
import com.artqiyi.dollmachine.views.adapter.MinePayRecordAdapter;
import com.artqiyi.dollmachine.views.widget.CommonFooter;
import com.artqiyi.dollmachine.views.widget.CommonHeader;
import com.artqiyi.dollmachine.views.widget.CustomGifHeader;
import com.artqiyi.dollmachine.views.widget.ToolBarView;
import com.artqiyi.dollmachine.views.widget.dialog.RxDialogList;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import timber.log.Timber;

/**
 * author: PengCheng
 * time: 2018/1/6 0006
 * desc: 充值记录
 */

public class MinePayRecordActivity extends BaseActivity<MinePayRecordPresenter> implements PresenterCallbackCollection.MinePayRecordActivityCallback{
    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.xRefreshView)
    XRefreshView mXRefreshView;

    private MinePayRecordAdapter mAdapter;

    private List<CoinRecordBean> mDollBeans = new ArrayList<>();

    private int pageNum = 1;

    private TextView tvCoin;
    @Override
    public MinePayRecordPresenter getPresenter() {
        return new MinePayRecordPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_mine_pay_record;
    }

    @Override
    protected void handleToolBar(ToolBarView toolBar) {
        super.handleToolBar(toolBar);
        toolBar.setTitleText("充值记录");
    }
    @Override
    public void initView(Bundle savedInstanceState) {
        mAdapter = new MinePayRecordAdapter(mDollBeans);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(MinePayRecordActivity.this));
        mRecyclerView.setAdapter(mAdapter);
        CommonHeader header = new CommonHeader(MinePayRecordActivity.this);
        header.setBackground(R.color.white);
        CommonFooter footer = new CommonFooter(MinePayRecordActivity.this);
        footer.setBackground(R.color.white);
        mXRefreshView.setCustomHeaderView(header);
        mXRefreshView.setCustomFooterView(footer);
        mXRefreshView.setAutoLoadMore(true);
        mXRefreshView.setPinnedTime(1000);
        mXRefreshView.setMoveForHorizontal(true);
        mXRefreshView.setPullLoadEnable(true);
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
                mXRefreshView.setLoadComplete(false);
                pageNum = 1;
                mPresenter.getPayRecords(pageNum);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                mPresenter.getPayRecords(pageNum);
            }
        });
    }

    @Override
    public void loadData() {
        super.loadData();
        mAdapter.setHeaderView(getHeader());
    }

    @Override
    public void reLoadData() {
        super.reLoadData();
//        mPresenter.getCoinTotal();
        pageNum = 1;
        mPresenter.getPayRecords(pageNum);
        if (null != tvCoin) tvCoin.setText(String.valueOf(UserInfoManager.getInstance().getUserInfoBean().getCoin_buy()));
    }

    @Override
    public void onGetMinePayRecordSuccess(List<CoinRecordBean> beans) {
        if (isFinishing()) return;
        if (null != mXRefreshView) {
            mXRefreshView.stopRefresh();
            mXRefreshView.stopLoadMore();
        }

        if (null != beans && beans.size() != 0) {
            if (pageNum == 1) mDollBeans.clear();
            mDollBeans.addAll(beans);
            mAdapter.notifyDataSetChanged();
            if (beans.size() < 20) mXRefreshView.setLoadComplete(true);
            pageNum++;
        }
    }

    @Override
    public void onGetMinePayRecordFail(String msg) {
        if (isFinishing()) return;
        if (null != mXRefreshView) {
            mXRefreshView.stopRefresh();
            mXRefreshView.setLoadComplete(true);
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCoinBuySuccess(List<CoinBuyBean> beans) {
        if (beans != null && beans.size() != 0) {
            doDialog(beans);
        } else {
            Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCoinBuyFail(String msg) {
        if (isFinishing()) return;
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onGetCoinTotalSuccess(CoinTotalBean bean) {
        if (isFinishing()) return;
        if (null == bean) return;
        if (null != tvCoin) tvCoin.setText(String.valueOf(bean.getCoin()));
    }

    @Override
    public void onGetCoinTotalFail(String msg) {
        showToast(msg);
    }

    private View getHeader() {
        View view = getLayoutInflater().inflate(R.layout.view_mine_doll_coin, (ViewGroup) mRecyclerView.getParent(), false);
        TextView tvCoinLabel = (TextView) view.findViewById(R.id.tvCoinLabel);
        tvCoin = (TextView) view.findViewById(R.id.tvCoin);
        tvCoinLabel.setText("总充值（金币）");
        TextView tvPay = (TextView) view.findViewById(R.id.tvPay);
        tvPay.setVisibility(View.VISIBLE);
        tvPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getCoinBuy();
            }
        });
        return view;
    }

    private void doDialog(List<CoinBuyBean> beans) {
        Timber.d("beans_num:" + beans.size());
        RxDialogList rxDialogList = new RxDialogList(this);//提示弹窗
        rxDialogList.getTitleView().setText("充娃娃币");
        rxDialogList.setData(beans);
        rxDialogList.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rxDialogList.cancel();
            }
        });
        rxDialogList.setOnListItemClick(new RxDialogList.OnClickListener() {
            @Override
            public void OnListItemClick(CoinBuyBean bean) {
                PayDialogManager.getInstance().getPay(bean);
                rxDialogList.cancel();
            }
        });
        rxDialogList.show();
    }
}
